package cn.edu.wfit.modules.learn.service.impl.competitionmanagement;

import java.util.ArrayList;
import java.util.List;
import cn.edu.wfit.common.core.utils.DateUtils;
import cn.edu.wfit.modules.learn.domain.*;
import cn.edu.wfit.modules.learn.domain.dto.TeamDetailDto;
import cn.edu.wfit.modules.learn.mapper.competitionmanagement.WfitCompetitionMapper;
import cn.edu.wfit.modules.learn.mapper.competitionmanagement.WfitCompetitionTeamrecordMapper;
import cn.edu.wfit.modules.learn.mapper.competitionmanagement.WfitCompetitionTeamregistrationMapper;
import cn.edu.wfit.modules.learn.service.competitionmanagement.IWfitCompetitionTeamregistrationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 团队赛队伍报名Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-28
 */
@Service
public class WfitCompetitionTeamregistrationServiceImpl implements IWfitCompetitionTeamregistrationService
{
    @Autowired
    private WfitCompetitionTeamregistrationMapper wfitCompetitionTeamregistrationMapper;

    @Autowired
    private WfitCompetitionMapper wfitCompetitionMapper;
    @Autowired
    private WfitCompetitionTeamrecordMapper wfitCompetitionTeamRecordMapper;
    /**
     * 查询团队赛队伍报名
     * 
     * @param id 团队赛队伍报名主键
     * @return 团队赛队伍报名
     */
    @Override
    public WfitCompetitionTeamregistration selectWfitCompetitionTeamregistrationById(Long id)
    {
        return wfitCompetitionTeamregistrationMapper.selectWfitCompetitionTeamregistrationById(id);
    }

    /**
     * 查询团队赛队伍报名列表
     * 
     * @param wfitCompetitionTeamregistration 团队赛队伍报名
     * @return 团队赛队伍报名
     */
    @Override
    public List<WfitCompetitionTeamregistration> selectWfitCompetitionTeamregistrationList(WfitCompetitionTeamregistration wfitCompetitionTeamregistration)
    {
        return wfitCompetitionTeamregistrationMapper.selectWfitCompetitionTeamregistrationList(wfitCompetitionTeamregistration);
    }

    /**
     * 新增团队赛队伍报名
     * 
     * @param wfitCompetitionTeamregistration 团队赛队伍报名
     * @return 结果
     */
    @Override
    public int insertWfitCompetitionTeamregistration(WfitCompetitionTeamregistration wfitCompetitionTeamregistration)
    {
        wfitCompetitionTeamregistration.setCreateTime(DateUtils.getNowDate());
        return wfitCompetitionTeamregistrationMapper.insertWfitCompetitionTeamregistration(wfitCompetitionTeamregistration);
    }

    /**
     * 修改团队赛队伍报名
     * 
     * @param wfitCompetitionTeamregistration 团队赛队伍报名
     * @return 结果
     */
    @Override
    public int updateWfitCompetitionTeamregistration(WfitCompetitionTeamregistration wfitCompetitionTeamregistration)
    {
        return wfitCompetitionTeamregistrationMapper.updateWfitCompetitionTeamregistration(wfitCompetitionTeamregistration);
    }

    /**
     * 批量删除团队赛队伍报名
     * 
     * @param ids 需要删除的团队赛队伍报名主键
     * @return 结果
     */
    @Override
    public int deleteWfitCompetitionTeamregistrationByIds(Long[] ids)
    {
        return wfitCompetitionTeamregistrationMapper.deleteWfitCompetitionTeamregistrationByIds(ids);
    }

    /**
     * 删除团队赛队伍报名信息
     * 
     * @param id 团队赛队伍报名主键
     * @return 结果
     */
    @Override
    public int deleteWfitCompetitionTeamregistrationById(Long id)
    {
        return wfitCompetitionTeamregistrationMapper.deleteWfitCompetitionTeamregistrationById(id);
    }

    /**
     * 根据学生id查询团队赛队伍报名列表
     * @param studentId
     * @return
     */
    @Override
    public List<TeamDetailDto> TeamListByStudentId(String studentId) {
        List<TeamDetailDto> teamDetailDtoList = new ArrayList<>();
        WfitCompetitionTeamregistration wfitCompetitionTeamregistration = new WfitCompetitionTeamregistration();
        //将id封装到查询条件中
        wfitCompetitionTeamregistration.setLeaderId(studentId);
        //根据学号查询报名表中的团队赛记录id
        List<WfitCompetitionTeamregistration> wfitCompetitionTeamregistrations = wfitCompetitionTeamregistrationMapper.selectWfitCompetitionTeamregistrationList(wfitCompetitionTeamregistration);

        //通过团队赛的记录id查询团队赛记录的信息,并封装到CompetitionDetailDto中
        for (WfitCompetitionTeamregistration teamregistration : wfitCompetitionTeamregistrations) {
            TeamDetailDto teamDetailDto = new TeamDetailDto();
            Long teamId = teamregistration.getId();
            teamDetailDto.setTeamId(teamId.intValue());
            //活动记录表信息
            WfitCompetitionTeamrecord teamrecord = wfitCompetitionTeamRecordMapper.selectWfitCompetitionTeamrecordById(teamregistration.getCompetitionId());
            //将记录表信息封装到TeamDetailDto中
            BeanUtils.copyProperties(teamrecord, teamDetailDto);
            //根据记录id查询竞赛表的信息
            WfitCompetition competition = wfitCompetitionMapper.selectWfitCompetitionById(teamrecord.getCompetitionId());
            //将竞赛表信息封装到TeamDetailDto中
            teamDetailDto.setCompetition(competition);
            //将封装好的TeamDetailDto添加到List中
            teamDetailDtoList.add(teamDetailDto);
        }
        return teamDetailDtoList;
    }

    @Override
    public List<TeamDetailDto> TeamMemberListByStudentId(String studentId) {
        List<TeamDetailDto> teamDetailDtoList = new ArrayList<>();
        List<WfitCompetitionTeamregistration> wfitCompetitionTeamregistrations = wfitCompetitionTeamregistrationMapper.selectWfitCompetitionTeamregistrationBystudentId(studentId);
        //通过团队赛的记录id查询团队赛记录的信息,并封装到CompetitionDetailDto中
        for (WfitCompetitionTeamregistration teamregistration : wfitCompetitionTeamregistrations) {
            TeamDetailDto teamDetailDto = new TeamDetailDto();
            Long teamId = teamregistration.getId();
            teamDetailDto.setTeamId(teamId.intValue());
            //活动记录表信息
            WfitCompetitionTeamrecord teamrecord = wfitCompetitionTeamRecordMapper.selectWfitCompetitionTeamrecordById(teamregistration.getCompetitionId());
            //将记录表信息封装到TeamDetailDto中
            BeanUtils.copyProperties(teamrecord, teamDetailDto);
            //根据记录id查询竞赛表的信息
            WfitCompetition competition = wfitCompetitionMapper.selectWfitCompetitionById(teamrecord.getCompetitionId());
            //将竞赛表信息封装到TeamDetailDto中
            teamDetailDto.setCompetition(competition);
            //将封装好的TeamDetailDto添加到List中
            teamDetailDtoList.add(teamDetailDto);
        }
        return teamDetailDtoList;
    }
}
